﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListItemEditor.UI
{
    public partial class BreadCrumb : UserControl
    {
        private List<Control> _links = new List<Control>();
        
        private int StartY { get; set; }
        private int StartX { get; set; }
        private int ItemSpacing { get; set; }

        public int ClickedIndex { get; set; }
        public object ClickedItem { get; set; }

        public event EventHandler<EventArgs> ItemClick;

        public BreadCrumb()
        {
            InitializeComponent();
            StartY = 5;
            StartX = 5;
            ItemSpacing = 10;
        }

        public object DataSource
        {
            get { return this.cmbDummy.DataSource; }
            set { this.cmbDummy.DataSource = value; DataSourceChanged(); }
        }

        public string DisplayMember
        {
            get { return this.cmbDummy.DisplayMember; }
            set { this.cmbDummy.DisplayMember = value; }
        }

        public string ValueMember
        {
            get { return this.cmbDummy.ValueMember; }
            set { this.cmbDummy.ValueMember = value; }
        }

        private void DataSourceChanged()
        {
            foreach (Control c in this._links) { c.Parent = null; c.Dispose(); }
            this._links.Clear();
            if (this.cmbDummy.Items == null) return;

            int count = 0, itemCount = this.cmbDummy.Items.Count;
            Point location = new Point(StartX, StartY);
            foreach (object item in this.cmbDummy.Items)
            {
                Label label;
                if (count == itemCount - 1)
                {
                    label = new Label();
                }
                else
                {
                    LinkLabel link = new LinkLabel();
                    link.Click += new EventHandler(link_Click);
                    label = link;
                }

                label.Text = this.cmbDummy.GetItemText(item);
                label.Tag = item;
                label.AutoSize = true;
                label.Parent = this;
                label.Location = location;
                this._links.Add(label);
                location.X += label.Width + ItemSpacing;

                count++;
            }
        }

        private void link_Click(object sender, EventArgs e)
        {
            Label label = sender as Label;
            if (label == null) return;

            int idx = this._links.IndexOf(label);
            if (idx < 0) return;

            this.ClickedIndex = idx;
            this.ClickedItem = label.Tag;

            OnLinkClicked(EventArgs.Empty);
        }

        protected virtual void OnLinkClicked (EventArgs e)
        {
            if (this.ItemClick != null) this.ItemClick(this, e);
        }
    }
}